<?php
/*
 $Id$ rpc.php
 ECG, http://www.gtf.16mb.com
 Copyright (c) 2012 Eugene.K
 
 This program is shareware software; all right reserved 
*/


/*
 * The admin side of the downloads plagin
*/

	require('includes/applications/products/classes/products.php');
	require('includes/classes/category_tree.php');
	require('includes/classes/image.php');
	require('../includes/classes/currencies.php');

	class osC_Products_Admin_rpc {
		public static function getAll() {
			global $_module, $osC_Currencies;

			if ( !isset($_GET['cID']) ) {
				$_GET['cID'] = '0';
			}

			if ( !isset($_GET['search']) ) {
				$_GET['search'] = '';
			}

			if ( !isset($_GET['page']) || !is_numeric($_GET['page']) ) {
				$_GET['page'] = 1;
			}

			$osC_Currencies = new osC_Currencies();

			if ( !empty($_GET['search']) ) {
				$result = osC_Products_Admin::find($_GET['search'], $_GET['cID'], $_GET['page']);
			} else {
				$result = osC_Products_Admin::getAll($_GET['cID'], $_GET['page']);
			}

			$result['rpcStatus'] = RPC_STATUS_SUCCESS;

			echo json_encode($result);
		}

		public static function getImages() {
			global $osC_Database, $_module;

			$osC_Image = new osC_Image_Admin();

			$result = array('entries' => array());

			$Qimages = $osC_Database->query('select id, image, default_flag from :table_products_images where products_id = :products_id order by sort_order');
			$Qimages->bindTable(':table_products_images', TABLE_PRODUCTS_IMAGES);
			$Qimages->bindInt(':products_id', $_GET[$_module]);
			$Qimages->execute();

			while ( $Qimages->next() ) {
				foreach ( $osC_Image->getGroups() as $group ) {
					$pass = true;

					if ( isset($_GET['filter']) && (($_GET['filter'] == 'originals') && ($group['id'] != '1')) ) {
						$pass = false;
					} elseif ( isset($_GET['filter']) && (($_GET['filter'] == 'others') && ($group['id'] == '1')) ) {
						$pass = false;
					}

					if ( $pass === true ) {
						$result['entries'][] = array($Qimages->valueInt('id'),
						$group['id'],
						$Qimages->value('image'),
						$group['code'],
						osc_href_link($osC_Image->getAddress($Qimages->value('image'), $group['code']), null, 'NONSSL', false, false, true),
						number_format(@filesize(DIR_FS_CATALOG . DIR_WS_IMAGES . 'products/' . $group['code'] . '/' . $Qimages->value('image'))),
						$Qimages->valueInt('default_flag'));
					}
				}
			}

			$result['rpcStatus'] = RPC_STATUS_SUCCESS;

			echo json_encode($result);
		}

		//*** START ***********************************************************************************
		public static function getFiles1()
		{
			global $osC_Database, $_module;
		
			$osC_Image = new osC_Image_Admin();
		
			$result = array('entries' => array());
		
			$Qpf = $osC_Database->query('select id, file_name, file_user_name, file_size, allow_download, deletedflag, date_added from :table_products_files where products_id = :products_id');
			$Qpf->bindTable(':table_products_files', TABLE_PRODUCTS_FILES);
			$Qpf->bindInt(':products_id', $_GET[$_module]);
			$Qpf->execute();
		
			$fileDescription=array();
			$fileNote=array();
			if ($Qpf->numberOfRows() > 0)
			{
				$pass = true;
				while ( $Qpf->next())
				{
					$QpfDesc = $osC_Database->query('select language_id, file_description, file_note from :table_products_files_description where file_id = :file_id');
					$QpfDesc->bindTable(':table_products_files_description', TABLE_PRODUCTS_FILE_DESCRIPTION);
					$QpfDesc->bindInt(':file_id', $Qpf->valueInt('id'));
					$QpfDesc->execute();
					if ($QpfDesc->numberOfRows() > 0)
					{
						while ( $QpfDesc->next())
						{
							$fileDescription[$QpfDesc->valueInt('language_id')]=$QpfDesc->value('file_description');
							$fileNote[$QpfDesc->valueInt('language_id')]=$QpfDesc->value('file_note');
						}
					}
					
					if ( $pass === true )
					{
						$result['entries'][] = array($Qpf->valueInt('id'),
								$Qpf->valueInt('allow_download'),
								$Qpf->value('file_name'),
								$Qpf->value('file_user_name'),
								$fileDescription,
								$Qpf->valueInt('file_size'),
								$Qpf->valueInt('deletedflag'),
								$fileNote);
					}
				}
			}
			$result['rpcStatus'] = RPC_STATUS_SUCCESS;
		
			echo json_encode($result);
		}
		
		public static function getLocalFiles()
		{
			$osC_DirectoryListing = new osC_DirectoryListing('../downloads/products/_upload', true);
			$osC_DirectoryListing->setIncludeDirectories(false);
		
			$result = array('entries' => array());
		
			foreach ( $osC_DirectoryListing->getFiles() as $file ) 
			{
				$result['entries'][] = $file['name'];
			}
		
			$result['rpcStatus'] = RPC_STATUS_SUCCESS;
		
			echo json_encode($result);
		}
		
		public static function assignLocalFiles()
		{
			global $osC_Database, $osC_Language, $_module;
		
			$osC_Image = new osC_Image_Admin();
		
			if (is_numeric($_GET[$_module]) && isset($_GET['files']))
			{
				$FileSize=0;
				$fileOriginalName='';
					
				foreach ($_GET['files'] as $file)
				{
					$fileOriginalName=basename($file);
		
					$file = basename($file);
					$tmp_file = time().'_'.$file;
		
					if (file_exists('../downloads/products/_upload/' . $file))
					{
						$FileSize=filesize('../downloads/products/_upload/' . $file);
						chmod('../downloads/products/_upload/' . $file, 0777);
						rename('../downloads/products/_upload/' . $file, '../downloads/products/' . $tmp_file);
						//@unlink('../downloads/products/_upload/' . $file);
							
						if (is_numeric($_GET[$_module]))
						{
							$Qfile = $osC_Database->query('insert into :table_products_files (products_id, file_name, file_user_name, file_size, allow_download, deletedflag, date_added) values (:products_id, :file_name, :file_user_name, :file_size, :allow_download, :deletedflag, :date_added)');
							$Qfile->bindTable(':table_products_files', TABLE_PRODUCTS_FILES);
							$Qfile->bindInt(':products_id', $_GET[$_module]);
							$Qfile->bindValue(':file_name', $tmp_file);
							$Qfile->bindValue(':file_user_name', $fileOriginalName);
							$Qfile->bindValue(':file_size', $FileSize);
							$Qfile->bindInt(':allow_download', '0');
							$Qfile->bindInt(':deletedflag', '0');
							$Qfile->bindRaw(':date_added', 'now()');
							$Qfile->execute();
		
							$file_id = $osC_Database->nextID();
								
							foreach ($osC_Language->getAll() as $l)
							{
								$QfileDesc = $osC_Database->query('insert into :table_products_files_description (file_id, language_id, file_description, file_note) values (:file_id, :language_id, :file_description, :file_note)');
								$QfileDesc->bindTable(':table_products_files_description', TABLE_PRODUCTS_FILE_DESCRIPTION);
								$QfileDesc->bindInt(':file_id', $file_id);
								$QfileDesc->bindInt(':language_id', $l['id']);
								$QfileDesc->bindValue(':file_description', '');
								$QfileDesc->bindValue(':file_note', '');
								$QfileDesc->execute();
							}
						}
					}
				}
			}
		
			$result = array('result' => 1,
					'rpcStatus' => RPC_STATUS_SUCCESS);
		
			echo json_encode($result);
		}
		
		public static function deleteProductFile() 
		{
			$osC_Image = new osC_Image_Admin();
		
			if ( isset($_GET['pfile']) ) 
			{
				$osC_Image->deleteFile($_GET['pfile']);
			}
		
			$result = array('result' => 1,
					'rpcStatus' => RPC_STATUS_SUCCESS);
		
			echo json_encode($result);
		}
		
		public static function SetPermissionProductFile()
		{
			global $osC_Database;
				
			if ( isset($_GET['pfile']) )
			{
				if (isset($_GET['perm']) && is_numeric($_GET['perm']) && $_GET['perm']>=0)
				{
					$QfilePerm = $osC_Database->query('update :table_products_files set allow_download=:allow_download where id = :id');
					$QfilePerm->bindTable(':table_products_files', TABLE_PRODUCTS_FILES);
					$QfilePerm->bindInt(':allow_download', $_GET['perm']);
					$QfilePerm->bindInt(':id', $_GET['pfile']);
					$QfilePerm->execute();
				}else
				{
					$Qpf = $osC_Database->query('select allow_download from :table_products_files where id = :id');
					$Qpf->bindTable(':table_products_files', TABLE_PRODUCTS_FILES);
					$Qpf->bindInt(':id', $_GET['pfile']);
					$Qpf->execute();
						
					if ($Qpf->numberOfRows() === 1)
					{
						$QfilePerm = $osC_Database->query('update :table_products_files set allow_download=:allow_download where id = :id');
						$QfilePerm->bindTable(':table_products_files', TABLE_PRODUCTS_FILES);
						$QfilePerm->bindInt(':id', $_GET['pfile']);
		
						if ($Qpf->valueInt('allow_download')==1)
						{
							$QfilePerm->bindInt(':allow_download', '0');
						}else
						{
							$QfilePerm->bindInt(':allow_download', '1');
						}
						$QfilePerm->execute();
					}
				}
			}
		
			$result = array('result' => 1,
					'rpcStatus' => RPC_STATUS_SUCCESS);
		
			echo json_encode($result);
		}
		
		public static function fileUpload1()
		{
			global $osC_Database, $osC_Language, $_module;
		
			$osC_Image = new osC_Image_Admin();
		
			if (is_numeric($_GET[$_module]))
			{
				$filEcg = array();
				$time1 = time();
				$FileSize=0;
				$fileOriginalName='';
		
				$products_file = new img_upload('products_file1');
		
				if ( $products_file->exists())
				{
					$doUpdate=false;
					$Qchecks_Files = $osC_Database->query('select id, file_name from :table_products_files where products_id = :products_id');
					$Qchecks_Files->bindTable(':table_products_files', TABLE_PRODUCTS_FILES);
					$Qchecks_Files->bindInt(':products_id', $_GET[$_module]);
					$Qchecks_Files->execute();
						
					$FileSize=$products_file->getSize();
					$products_file->set_destination(realpath('../downloads/products'));
						
					if ( $products_file->parse() && $products_file->save() )
					{
						$filEcg[] = $time1.'_'.$products_file->filename;
						$fileOriginalName=$products_file->filename;
							
						foreach ($filEcg as $tmp_file1)
						{
							$Qfile = $osC_Database->query('insert into :table_products_files (products_id, file_name, file_user_name, file_size, allow_download, deletedflag, date_added) values (:products_id, :file_name, :file_user_name, :file_size, :allow_download, :deletedflag, :date_added)');
							$Qfile->bindTable(':table_products_files', TABLE_PRODUCTS_FILES);
							$Qfile->bindInt(':products_id', $_GET[$_module]);
							$Qfile->bindValue(':file_name', $tmp_file1);
							$Qfile->bindValue(':file_user_name', $fileOriginalName);
							$Qfile->bindValue(':file_size', $FileSize);
							$Qfile->bindInt(':allow_download', '0');
							$Qfile->bindInt(':deletedflag', '0');
							$Qfile->bindRaw(':date_added', 'now()');
							//$Qfile->setLogging($_SESSION['module'], $_GET[$_module]);
							$Qfile->execute();
		
							$file_id = $osC_Database->nextID();
								
							foreach ($osC_Language->getAll() as $l)
							{
								$QfileDesc = $osC_Database->query('insert into :table_products_files_description (file_id, language_id, file_description, file_note) values (:file_id, :language_id, :file_description, :file_note)');
									
								$QfileDesc->bindTable(':table_products_files_description', TABLE_PRODUCTS_FILE_DESCRIPTION);
								$QfileDesc->bindInt(':file_id', $file_id);
								$QfileDesc->bindInt(':language_id', $l['id']);
								$QfileDesc->bindValue(':file_description', '');
								$QfileDesc->bindValue(':file_note', '');
								$QfileDesc->execute();
							}
						}
					}
				}
			}
		
			$result = array('result' => 1,
					'rpcStatus' => RPC_STATUS_SUCCESS);
		
			echo json_encode($result);
		}
		//*** END ***********************************************************************************
		
		
		public static function getLocalImages() {
			$osC_DirectoryListing = new osC_DirectoryListing('../images/products/_upload', true);
			$osC_DirectoryListing->setCheckExtension('gif');
			$osC_DirectoryListing->setCheckExtension('GIF');
			$osC_DirectoryListing->setCheckExtension('jpg');
			$osC_DirectoryListing->setCheckExtension('JPG');
			$osC_DirectoryListing->setCheckExtension('jpeg');
			$osC_DirectoryListing->setCheckExtension('JPEG');
			$osC_DirectoryListing->setCheckExtension('png');
			$osC_DirectoryListing->setCheckExtension('PNG');
			$osC_DirectoryListing->setIncludeDirectories(false);

			$result = array('entries' => array());

			foreach ( $osC_DirectoryListing->getFiles() as $file ) {
				$result['entries'][] = $file['name'];
			}

			$result['rpcStatus'] = RPC_STATUS_SUCCESS;

			echo json_encode($result);
		}

		public static function assignLocalImages() {
			global $osC_Database, $_module;

			$osC_Image = new osC_Image_Admin();

			if ( is_numeric($_GET[$_module]) && isset($_GET['files']) ) {
				$default_flag = 1;

				$Qcheck = $osC_Database->query('select id from :table_products_images where products_id = :products_id and default_flag = :default_flag limit 1');
				$Qcheck->bindTable(':table_products_images', TABLE_PRODUCTS_IMAGES);
				$Qcheck->bindInt(':products_id', $_GET[$_module]);
				$Qcheck->bindInt(':default_flag', 1);
				$Qcheck->execute();

				if ( $Qcheck->numberOfRows() === 1 ) {
					$default_flag = 0;
				}

				$y=1;
				foreach ( $_GET['files'] as $file ) {
					if ($y>1){
						$default_flag = 0;
					}
					$file = basename($file);
					$tmp_file = time().'_'.$file;

					if ( file_exists('../images/products/_upload/' . $file) ) {
						copy('../images/products/_upload/' . $file, '../images/products/originals/' . $tmp_file);
						@unlink('../images/products/_upload/' . $file);

						if ( is_numeric($_GET[$_module]) ) {
							$Qimage = $osC_Database->query('insert into :table_products_images (products_id, image, default_flag, sort_order, date_added) values (:products_id, :image, :default_flag, :sort_order, :date_added)');
							$Qimage->bindTable(':table_products_images', TABLE_PRODUCTS_IMAGES);
							$Qimage->bindInt(':products_id', $_GET[$_module]);
							$Qimage->bindValue(':image', $tmp_file);
							$Qimage->bindInt(':default_flag', $default_flag);
							$Qimage->bindInt(':sort_order', 0);
							$Qimage->bindRaw(':date_added', 'now()');
							$Qimage->setLogging($_SESSION['module'], $_GET[$_module]);
							$Qimage->execute();

							foreach ( $osC_Image->getGroups() as $group ) {
								if ( $group['id'] != '1' ) {
									$osC_Image->resize($tmp_file, $group['id']);
								}
							}
						}
					}
					$y=$y+1;
				}
			}

			$result = array('result' => 1,
			'rpcStatus' => RPC_STATUS_SUCCESS);

			echo json_encode($result);
		}

		public static function setDefaultImage() {
			$osC_Image = new osC_Image_Admin();

			if ( isset($_GET['image']) ) {
				$osC_Image->setAsDefault($_GET['image']);
			}

			$result = array('result' => 1,
			'rpcStatus' => RPC_STATUS_SUCCESS);

			echo json_encode($result);
		}

		public static function deleteProductImage() {
			$osC_Image = new osC_Image_Admin();

			if ( isset($_GET['image']) ) {
				$osC_Image->delete($_GET['image']);
			}

			$result = array('result' => 1,
			'rpcStatus' => RPC_STATUS_SUCCESS);

			echo json_encode($result);
		}
		
		public static function reorderImages() {
			$osC_Image = new osC_Image_Admin();

			if ( isset($_GET['image']) ) {
				$osC_Image->reorderImages($_GET['image']);
			}

			$result = array('result' => 1,
			'rpcStatus' => RPC_STATUS_SUCCESS);

			echo json_encode($result);
		}

		public static function fileUpload() {
			global $osC_Database, $_module;

			$p = $_SESSION['products_images'];
			for ($x=1; $x<$p; $x++){
				$osC_Image = new osC_Image_Admin();

				if ( is_numeric($_GET[$_module]) ) {
					$products_image = new img_upload('products_image'.$x);
					$products_image->set_extensions(array('gif', 'jpg', 'jpeg', 'png', 'GIF', 'JPG', 'JPEG', 'PNG'));

					if ( $products_image->exists() ) {
						$products_image->set_destination(realpath('../images/products/originals'));

						if ( $products_image->parse() && $products_image->save() ) {
							$default_flag = 1;

							$Qcheck = $osC_Database->query('select id from :table_products_images where products_id = :products_id and default_flag = :default_flag limit 1');
							$Qcheck->bindTable(':table_products_images', TABLE_PRODUCTS_IMAGES);
							$Qcheck->bindInt(':products_id', $_GET[$_module]);
							$Qcheck->bindInt(':default_flag', 1);
							$Qcheck->execute();

							if ( $Qcheck->numberOfRows() === 1 ) {
								$default_flag = 0;
							}
							$time = time();
							$Qimage = $osC_Database->query('insert into :table_products_images (products_id, image, default_flag, sort_order, date_added) values (:products_id, :image, :default_flag, :sort_order, :date_added)');
							$Qimage->bindTable(':table_products_images', TABLE_PRODUCTS_IMAGES);
							$Qimage->bindInt(':products_id', $_GET[$_module]);
							$Qimage->bindValue(':image', $time.'_'.$products_image->filename);
							$Qimage->bindInt(':default_flag', $default_flag);
							$Qimage->bindInt(':sort_order', 0);
							$Qimage->bindRaw(':date_added', 'now()');
							$Qimage->setLogging($_SESSION['module'], $_GET[$_module]);
							$Qimage->execute();

							foreach ( $osC_Image->getGroups() as $group ) {
								if ( $group['id'] != '1' ) {
									$osC_Image->resize($time.'_'.$products_image->filename, $group['id']);
								}
							}
						}
					}
				}

				$result = array('result' => 1,
				'rpcStatus' => RPC_STATUS_SUCCESS);

				echo json_encode($result);
			}
		}		
	}
?>
